package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.domain.Seat;
import com.ruoyi.system.mapper.RoomMapper;
import com.ruoyi.system.mapper.SeatMapper;
import com.ruoyi.system.service.IRoomService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 自习室Service业务层处理
 *
 * @author ...
 * @date 2022-03-17
 */
@Service
public class RoomServiceImpl implements IRoomService {
    @Resource
    private RoomMapper roomMapper;

    @Resource
    private SeatMapper seatMapper;

    /**
     * 查询自习室
     *
     * @param id 自习室主键
     * @return 自习室
     */
    @Override
    public Room selectRoomById(Integer id) {
        return roomMapper.selectRoomById(id);
    }

    /**
     * 查询自习室列表
     *
     * @param room 自习室
     * @return 自习室
     */
    @Override
    public List<Room> selectRoomList(Room room) {
        return roomMapper.selectRoomList(room);
    }

    /**
     * 新增自习室
     *
     * @param room 自习室
     * @return 结果
     */
    @Override
    public int insertRoom(Room room) {
        room.setSpecifications(room.getRow() + "-" + room.getColumn());


        if (roomMapper.insertRoom(room) > 0) {
            // 自动生成座位
            for (int i = 1; i <= room.getRow(); i++) {
                for (int j = 1; j <= room.getColumn(); j++) {
                    Seat seat = new Seat();
                    seat.setRoomId(String.valueOf(room.getId()));
                    seat.setRoomName(room.getName());
                    seat.setRow(i);
                    seat.setColumn(j);
                    seat.setStatus("空闲");
                    seat.setTypeId("4");
                    if (room.getType().equals("豪华")){
                        seat.setType("VIP座位");
                    }else{
                        seat.setType("普通座位");
                    }
                    seat.setCreateTime(DateUtils.getNowDate());
                    seat.setCreateUser(SecurityUtils.getUserId().intValue());
                    seatMapper.insertSeat(seat);
                }
            }
        }
        return room.getId();
    }

    /**
     * 修改自习室
     *
     * @param room 自习室
     * @return 结果
     */
    @Override
    public int updateRoom(Room room) {
        room.setUpdateTime(DateUtils.getNowDate());
        return roomMapper.updateRoom(room);
    }

    /**
     * 批量删除自习室
     *
     * @param ids 需要删除的自习室主键
     * @return 结果
     */
    @Override
    public int deleteRoomByIds(Integer[] ids) {
        return roomMapper.deleteRoomByIds(ids);
    }

    /**
     * 删除自习室信息
     *
     * @param id 自习室主键
     * @return 结果
     */
    @Override
    public int deleteRoomById(Integer id) {
        return roomMapper.deleteRoomById(id);
    }
}
